<template>
		<view>
			<scroll-view class="scroll" scroll-x>
				<view class="item"  :class="navid==i.id?'active':''" 
				 v-for="(i,index) in nav " :key="index" @click="clickNav(i.id)">{{i.classname}}</view>
			</scroll-view>
			<view class="content">
				<view v-for="(i,index) in newsarr" :key="index" @click="toDetail(i.id)">
					<newsbox :item="i"></newsbox>
				</view>
			</view>
			<view v-if="newsarr.length==0" class="nodata">
				<image src="../../static/nodata.png" mode="aspectFill"></image>
				<view class="text">暂无数据</view>
			</view>
			<view class="desc">
				<view v-if="loading==1">加载中</view>
				<view v-if="loading==2">没有更多数据了</view>
			</view>
		</view>
</template>

<script>
	export default {
		data() {
			return {
				title: 'Hello',
				navid:50,
				nav:[],//导航栏
				newsarr:[],
				currentPage:1,//当前页,
				loading:0//状态 0:默认 1:加载中 2:没有更多了
			}
		},
		onLoad() {
			this.getNav()
			this.getListData()
		},
		onReachBottom() {
			console.log("我触底了")
			this.currentPage++
			this.loading=1
			this.getListData(this.navid)
			this.loading=0
		},
		methods: {
			clickNav(index){
				this.currentPage=1
				this.newsarr=[]
				this.navid=index
				this.getListData(index)
			},
			toDetail(index){
				uni.navigateTo({
					url:`/pages/detail/detail?cid=${this.navid}&id=${index}`,
				})
			},
			//获得新闻栏目数据
			getNav(){
				uni.request({
					url:"https://ku.qingnian8.com/dataApi/news/navlist.php",
					success: (res) => {
						//console.log(res)
						this.nav=res.data
					}
				})
			},
			//获取新闻数据
			getListData(id=50){
				uni.request({
					url:"https://ku.qingnian8.com/dataApi/news/newslist.php",
					data:{
						cid:id,
						page:this.currentPage
					},
					success: (res) => {
						this.newsarr=[...this.newsarr,...res.data]
						if(res.data.length==0){
							this.loading=2
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
.scroll{
	height: 75rpx;
	background-color: #f7f8fa;
	white-space: nowrap;
	position: flex;
	top:0;
	left: 0;
	top:var(--window-top);
	.item{
		display: inline;
		font-size: 38rpx;
		margin-left: 15rpx;
		padding: 40rpx;
		&.active{
			color:#31c27c ;
		}
	}
}
.nodata{
	width: 750rpx;
	height: 150rpx;
	image{
		width: 100%;
	}
	.text{
		margin-left: 45%;
		align-items: center;
	}
}

.content{
	margin-top: 30rpx;
}

.desc{
	text-align: center;
}
</style>
